#include "keypadhandler.h"
#if defined(Q_WS_QWS)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <QDebug>
#include <QString>

#include <QSocketNotifier>
#include <QStringList>

#include <qplatformdefs.h>

#include <errno.h>
#include <termios.h>

#include <linux/kd.h>
#include <linux/input.h>

KeypadHandler::KeypadHandler(const QString &device, QObject *parent)
    : QObject(parent), QWSKeyboardHandler()
{
    setObjectName("Keypad Handler");
    qDebug() << "Open kbd"<<device;
    this->kbdFd = ::open(device.toAscii().constData(), O_RDONLY);
    if(this->kbdFd < 0)
        return;

    this->notifier = new QSocketNotifier(this->kbdFd, QSocketNotifier::Read, this);
    connect(this->notifier, SIGNAL(activated(int)), this, SLOT(kbdReadyRead()));
}

KeypadHandler::~KeypadHandler()
{
    if (this->kbdFd >= 0)
        ::close(this->kbdFd);
}

void KeypadHandler::kbdReadyRead()
{

    struct ::input_event buffer[32];

    int n = 0;

    forever {
        n = QT_READ(this->kbdFd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);

        if (n == 0) {
            qWarning("Got EOF from the input device.");
            return;
        } else if (n < 0 && (errno != EINTR && errno != EAGAIN)) {
            qWarning("Could not read from input device: %s", strerror(errno));
            return;
        } else if (n % sizeof(buffer[0]) == 0) {
            break;
        }
    }

    Qt::KeyboardModifiers modifiers = Qt::NoModifier;

    n /= sizeof(buffer[0]);

    for (int i = 0; i < n; ++i) {
        if (buffer[i].type != EV_KEY)
            continue;
        quint16 type=buffer[i].type;
        quint16 code = buffer[i].code;
        qint32 value = buffer[i].value;

        int unicode = 0xffff;
        int keycode = 0;
        if(type!=1) continue;
        switch (code) {
        case KEY_0:
            keycode = Qt::Key_0;
            //unicode = '0';
            break;
        case KEY_1:
            keycode = Qt::Key_1;
            //unicode = '1';
            break;
        case KEY_2:
            keycode = Qt::Key_2;
            //unicode = '2';
            break;
        case KEY_3:
            keycode = Qt::Key_3;
            //unicode = '3';
            break;
        case KEY_4:
            keycode = Qt::Key_4;
            //unicode = '4';
            break;
        case KEY_5:
            keycode = Qt::Key_5;
            //unicode = '5';
            break;
        case KEY_6:
            keycode = Qt::Key_6;
            //unicode = '6';
            break;
        case KEY_7:
            keycode = Qt::Key_7;
            //unicode = '7';
            break;
        case KEY_8:
            keycode = Qt::Key_8;
            //unicode = '8';
            break;
        case KEY_9:
            keycode = Qt::Key_9;
            //unicode = '9';
            break;
        case KEY_DOT:
            keycode = Qt::Key_Period;
            //unicode = '.';
            break;
        case KEY_OK:
            keycode = Qt::Key_Equal	;
            //unicode = '=';
            break;
        case KEY_BACK:
            keycode = Qt::Key_Backspace	;
            //unicode = '=';
            break;
        case KEY_CANCEL:
            keycode = Qt::Key_Escape	;
            //unicode = 'e';
            break;
        case KEY_F1:
            keycode = Qt::Key_Asterisk		;
            //unicode = '*';
            break;
        case KEY_F2:
            keycode = Qt::Key_Enter		;
            //unicode = 'o';
            break;
        case KEY_TAB:
            keycode = Qt::Key_Tab;
            //unicode = 't';
            break;
        case KEY_MODE:
            keycode = Qt::Key_F12;
            //unicode = 'm';
            break;
        case KEY_ENTER:
            keycode = Qt::Key_Insert;
            //unicode = 'i';
            break;
        case KEY_UP:
            keycode = Qt::Key_Up;
            //unicode = 'u';
            break;
        case KEY_DOWN:
            keycode = Qt::Key_Down;
            //unicode = 'd';
            break;
        case KEY_LEFT:
            keycode = Qt::Key_Left;
            //unicode = 'l';
            break;
        case KEY_RIGHT:
            keycode = Qt::Key_Right;
            //unicode = 'r';
            break;
        default:
            keycode=Qt::Key_unknown;
            qWarning("unknown key.");
            break;
        }
        if(value==0)
            this->processKeyEvent(unicode,keycode, modifiers, value==0, false);
    }

}
#endif
